package com.example.yin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yin.common.R;
import com.example.yin.mapper.SongMapper;
import com.example.yin.model.domain.Song;
import com.example.yin.model.request.SongRequest;
import com.example.yin.service.SongService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song> implements SongService {

    @Autowired
    private SongMapper songMapper;

    //所有歌曲
    @Override
    public R allSong() {
        return R.success(null, songMapper.selectList(null));
    }

    //添加歌曲
    @Override
    public R addSong(SongRequest addSongRequest, MultipartFile mpfile) {
        Song song = new Song();
        BeanUtils.copyProperties(addSongRequest, song);
        String pic = "/img/songPic/tubiao.jpg";
        String fileName = mpfile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "song";
        File file1 = new File(filePath);
        if (!file1.exists()) {
            if (!file1.mkdir()) {
                return R.fatal("创建文件失败");
            }
        }
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String storeUrlPath = "/song/" + fileName;
        try {
            mpfile.transferTo(dest);
        } catch (IOException e) {
            return R.fatal("上传失败" + e.getMessage());
        }
        song.setCreateTime(new Date());
        song.setUpdateTime(new Date());
        song.setPic(pic);
        song.setUrl(storeUrlPath);
        if (songMapper.insert(song) > 0) {                     //如果insert方法的返回值大于0
            return R.success("上传成功", storeUrlPath); //返回上传成功信息
        } else {
            return R.error("上传失败");                         //否则，返回上传失败的信息
        }
    }

    //更新歌曲信息
    @Override
    public R updateSongMsg(SongRequest updateSongRequest) {
        Song song = new Song();
        BeanUtils.copyProperties(updateSongRequest, song);
        if (songMapper.updateById(song) > 0) {         //如果updateById方法的返回值大于0
            return R.success("修改成功");               //返回修改成功的信息
        } else {
            return R.error("修改失败");                 //否则，返回修改失败的信息
        }
    }

    @Override
    public R updateSongUrl(MultipartFile urlFile, int id) {
        String fileName = urlFile.getOriginalFilename();          //获取上传文件的原始文件名
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "song";
        File file1 = new File(filePath);             //创建了一个File对象，表示目录路径
        if (!file1.exists()) {                       //判断目录是否存在
            if (!file1.mkdir()) {                    //如果目录不存在，则尝试创建目录
                return R.fatal("创建目的文件夹失败");   //如果创建目录失败，返回一个失败的R对象，表示创建目录失败
            }
        }
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String storeUrlPath = "/song/" + fileName;
        try {
            urlFile.transferTo(dest);                     //将上传的文件mpfile保存到目标文件dest中
        } catch (IOException e) {                         //捕捉可能出现的IOException异常
            return R.fatal("更新失败" + e.getMessage());   //返回一个失败的R对象，表示更新失败，并包含了异常信息
        }
        Song song = new Song();
        song.setId(id);
        song.setUrl(storeUrlPath);
        if (songMapper.updateById(song) > 0) {                     //如果updateById方法的返回值大于0
            return R.success("更新成功", storeUrlPath);     //返回更新成功的信息
        } else {
            return R.error("更新失败");                             //否则，返回更新失败的信息
        }
    }

    @Override
    public R updateSongPic(MultipartFile urlFile, int id) {
        String fileName = System.currentTimeMillis() + urlFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img" + System.getProperty("file.separator") + "songPic";
        File file1 = new File(filePath);
        if (!file1.exists()) {                         //判断目录是否存在
            if (!file1.mkdir()) {                      //如果目录不存在，则尝试创建目录
                return R.fatal("创建文件夹失败");        //如果创建目录失败，返回一个失败的R对象，表示创建目录失败
            }
        }

        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String storeUrlPath = "/img/songPic/" + fileName;
        try {
            urlFile.transferTo(dest);                      //将上传的文件mpfile保存到目标文件dest中
        } catch (IOException e) {                          //捕捉可能出现的IOException异常
            return R.fatal("上传失败" + e.getMessage());    //返回一个失败的R对象，表示上传失败，并包含了异常信息
        }
        Song song = new Song();
        song.setId(id);
        song.setPic(storeUrlPath);
        if (songMapper.updateById(song) > 0) {                 //updateById方法的返回值是否大于0
            return R.success("上传成功", storeUrlPath); //如果是，返回上传成功的信息
        } else {
            return R.error("上传失败");                         //否则，返回上传失败的信息
        }
    }

    //删除歌曲
    @Override
    public R deleteSong(Integer id) {
        if (songMapper.deleteById(id) > 0) {      //判断deleteById方法的返回值是否大于0
            return R.success("删除成功");          //若是，返回删除成功的信息
        } else {
            return R.error("删除失败");            //否则，返回删除失败的信息
        }
    }

    //通过歌手Id查询歌曲
    @Override
    public R songOfSingerId(Integer singerId) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("singer_id", singerId);
        return R.success(null, songMapper.selectList(queryWrapper));
    }

    //通过Id查询歌曲信息
    @Override
    public R songOfId(Integer id) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return R.success(null, songMapper.selectList(queryWrapper));
    }

    //通过歌手姓名查询歌曲信息
    @Override
    public R songOfSingerName(String name) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return R.success(null, songMapper.selectList(queryWrapper));
    }

    @Override
    public R checkVipSong(int id) {
        QueryWrapper<Song> qw = new QueryWrapper<>();
        qw.eq("id", id);
        if (songMapper.selectOne(qw).getVip() == 1)
            return R.success("会员歌曲");
        else
            return R.error("非会员歌曲");
    }

    @Override
    public R addVipSong(int id) {
        QueryWrapper<Song> qw = new QueryWrapper<>();
        qw.eq("id", id);
        Song song = new Song();
        song.setVip(1);
        if (songMapper.selectById(id).getVip() == 1) {
            return R.error("已是会员歌曲，不可重复添加");
        } else if (songMapper.update(song, qw) > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @Override
    public R dellVipSong(int id) {
        QueryWrapper<Song> qw = new QueryWrapper<>();
        qw.eq("id", id);
        Song song = new Song();
        song.setVip(0);
        if (songMapper.selectById(id).getVip() == 0) {
            return R.error("不是会员歌曲");
        } else if (songMapper.update(song, qw) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }
}
